#include "../includes/IPC.h"
#include <iostream>
#include <chrono>
#include <thread>

int main() {
    IpcClient& client = IpcClient::getInstance("/tmp/test_ipc.sock");
    
    if (client.conn_to_server() == -1) {
        std::cerr << "Failed to connect to server" << std::endl;
        return -1;
    }
    
    std::cout << "Connected to server" << std::endl;
    
    // 客户端发送消息并接收回复
    char recv_buf[1024];
    char send_buf[1024];
    
    for (int i = 0; i < 5; ++i) {
        // 发送消息
        std::string msg = "Hello from client " + std::to_string(i);
        strcpy(send_buf, msg.c_str());
        
        int send_size = client.send_data(client.fd, msg.length(), send_buf);
        if (send_size <= 0) {
            std::cerr << "Send failed" << std::endl;
            break;
        }
        
        std::cout << "Client sent: " << msg << std::endl;
        
        // 接收回复
        int recv_size = client.receive_data(client.fd, 1024, recv_buf);
        if (recv_size <= 0) {
            std::cerr << "Receive failed" << std::endl;
            break;
        }
        
        recv_buf[recv_size] = '\0';
        std::cout << "Client received: " << recv_buf << std::endl;
        
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }
    
    std::cout << "Client finished" << std::endl;
    return 0;
}